import * as Router from 'koa-router'
import { CityJS, CountryJS } from '../db/sequelize_origin'


const router = new Router({ prefix: '/js' })
  .get('/countries', async ctx => {
    try {
      // await sequelize.authenticate()
      // ctx.body = await CityTS.findOne({ include: [ CountryTS ] })
      ctx.body = await CountryJS.findAndCount({ include: [ CityJS ], limit: 50 })
    } catch (e) {
      console.log(e)
      ctx.body = 'countries fail'
    }
  })
  .get('/cities', async ctx => {
    try {
      ctx.body = await CityJS.findAndCount({ include: [ CountryJS ], limit: 50 })
    } catch (e) {
      console.log(e)
      ctx.body = 'cities fail'
    }
  })

export const js = [ router.routes(), router.allowedMethods() ]
